import {
  FileFragmentUploadReturnValue,
  createDownloadLinkReturnValue,
  getAllDirectoriesReturnValue,
  getCurrentDirectoryReturnValue,
  listFilesReturnValue,
} from "@/types/home";
import { GeneralReturnValue } from "@/types/general";
import http from "@/utils/axios";

/**
 * 文件列表
 * @param data 分类,文件父id,文件名,页码,分页大小
 * @returns
 */
export const listFilesAPI = (data: {
  category: string;
  filePid: string;
  fileNameFuzzy: string;
  pageNo: string;
  pageSize: string;
}) =>
  http<listFilesReturnValue>({
    url: "/file/loadDataList",
    method: "POST",
    data,
  });

/**
 * 文件分片上传
 * @param data 文件ID,文件流,文件名,文件父id,文件MD5值,当前分片索引,总分片数
 * @returns
 */
export const FileFragmentUploadAPI = (data: {
  fileId: string;
  file: string;
  fileName: string;
  filePid: string;
  fileMd5: string;
  chunkIndex: string;
  chunks: string;
}) =>
  http<FileFragmentUploadReturnValue>({
    url: "/file/uploadFile",
    method: "POST",
    data,
  });

// TODO
/**
 * 显示封面
 * @returns
 */
export const showCoverAPI = (imageFolder: string, imageName: string) =>
  http<any>({
    url: `/file/getImage/${imageFolder}/${imageName}`,
    method: "GET",
  });

// TODO
/**
 * 获取文件信息
 * @returns
 */
export const getFileInformationAPI = (fileId: string) =>
  http<any>({
    url: `/file/getFile/${fileId}`,
    method: "GET",
  });

/**
 * 新建目录
 * @param data 文件父id,目录名
 * @returns
 */
export const newDirectoryAPI = (data: { filePid: string; fileName: string }) =>
  http<GeneralReturnValue>({
    url: "/file/newFoloder",
    method: "POST",
    data,
  });

/**
 * 获取当前目录
 * @param data 完整路径
 * @returns
 */
export const getCurrentDirectoryAPI = (data: { path: string }) =>
  http<getCurrentDirectoryReturnValue>({
    url: "/file/getFolderInfo",
    method: "POST",
    data,
  });

/**
 * 文件重命名
 * @param data 文件ID,文件名
 * @returns
 */
export const fileRenameAPI = (data: { fileId: string; fileName: string }) =>
  http<GeneralReturnValue>({
    url: "/file/rename",
    method: "POST",
    data,
  });

/**
 * 获取所有目录
 * @param data 文件父id,当前目录PID
 * @returns
 */
export const getAllDirectoriesAPI = (data: {
  filePid: string;
  currentFileIds: string;
}) =>
  http<getAllDirectoriesReturnValue>({
    url: "/file/loadAllFolder",
    method: "POST",
    data,
  });

/**
 * 修改文件目录、移动文件
 * @param data 所选文件ID,文件父id
 * @returns
 */
export const modifyFileDirectoriesAndMoveFilesAPI = (data: {
  fileIds: string;
  filePid: string;
}) =>
  http<GeneralReturnValue>({
    url: "/file/changeFileFolder",
    method: "POST",
    data,
  });

/**
 * 创建下载链接
 * @param data
 * @returns
 */
export const createDownloadLinkAPI = (fileId: string) =>
  http<createDownloadLinkReturnValue>({
    url: `/file/createDownloadUrl/${fileId}`,
    method: "POST",
  });

/**
 * 下载文件
 * @param data
 * @returns
 */
export const downloadFileAPI = (code: string) =>
  http<createDownloadLinkReturnValue>({
    url: `/file/download/${code}`,
    method: "POST",
  });

/**
 * 删除文件
 * @param data 需要删除的文件ID
 * @returns
 */
export const deleteFilesAPI = (data: { fileIds: string }) =>
  http<GeneralReturnValue>({
    url: "/file/delFile",
    method: "POST",
    data,
  });
